<?php
use Entities\Obras;
use Entities\Funcionario;

include_once '../templates/topo.php';

$id= '';

if(isset($_REQUEST['id']))
	$id = $_REQUEST['id'];
	
$obras = $em->find("Entities\Obras", $id);
if(empty($obras))
	$obras = new Obras();
?>
	<div class="tabs center">
			<ul>
				<li><a href="#tabs-1">Obras</a></li>
			</ul>
			<div id="tabs-1">
				<div cols='3'>
					<div class='field' >
						<label>Nome Obra</label>
						<input type="hidden" value="<?php echo $id;?>" id='hidId' />
						<input type='text' id='txtNomeObra' value='<?php echo $obras->getNomeObra();?>' size='45' onkeypress="mascara(this,maiusculo)" />
					</div>
					<div class='field'>
						<label>Eng. Responsavel</label>
						<select id='selEngResponsavel' >
							<option value='0'>Selecione</option>
							<?php 
								$queryEngResponsavel = $em->createQuery("select c from Entities\Funcionario c where c.isAdm = 1");
								$engenheiros = $queryEngResponsavel->getResult();

								foreach ($engenheiros as $engenheiro) {
									if(isset($_REQUEST['id'])){
										$selected = "";
										if ($engenheiro->getId() == $obras->getFuncionario()->getId()){
											$selected = "selected='selected'";
										}
										echo "<option value='".$engenheiro->getId()."'".$selected." >".$engenheiro->getNome()."</option>";
									}else{
										echo "<option value='".$engenheiro->getId()."' >".$engenheiro->getNome()."</option>";
									}
								}
							?>
						</select>
					</div>
					<div class='field'>
						<label>CEI Obra</label>
						<input type='text' id='txtRegObra' value='<?php echo $obras->getRegistroObra();?>' size='30' onkeypress="mascara(this,maiusculo)" />
					</div>
					<div class='field'>
						<label>Endereco</label>
						<input type='text' id='txtEndObra' value='<?php echo $obras->getEndObra();?>' size='45' onkeypress="mascara(this,maiusculo)" />
					</div>
					<div class='field'>
						<label>Nro</label>
						<input type='text' id='txtNroEndObra' value='<?php echo $obras->getNroEndObra();?>' size='10' onkeypress="mascara(this,maiusculo)" />
					</div>
					<div class='field'>
						<label>Bairro</label>
						<input type='text' id='txtBairroObra' value='<?php echo $obras->getBairroObra();?>' size='30' onkeypress="mascara(this,maiusculo)" />
					</div>
					<div class='field'>
						<label>Cidade</label>
						<input type='text' id='txtCidadeObra' value='<?php echo $obras->getCidadeObra();?>' size='45' onkeypress="mascara(this,maiusculo)" />
					</div>
					<div class='field'>
						<label>Estado</label>
						<select id="selEstado">
							<?php if($id == ''){
//								$seleciona = "selected='selected";
							}?>
							<option value="0">Selecione</option>
							<option value="ac" <?php if($obras->getUfObra() == 'ac'){echo "selected='selected'";} $seleciona;?> >Acre</option>
							<option value="al" <?php if($obras->getUfObra() == 'al'){echo "selected='selected'";} ?> >Alagoas</option>
							<option value="am" <?php if($obras->getUfObra() == 'am'){echo "selected='selected'";} ?> >Amazonas</option>
							<option value="ap" <?php if($obras->getUfObra() == 'ap'){echo "selected='selected'";} ?> >Amap&aacute;</option>
							<option value="ba" <?php if($obras->getUfObra() == 'ba'){echo "selected='selected'";} ?> >Bahia</option>
							<option value="ce" <?php if($obras->getUfObra() == 'ce'){echo "selected='selected'";} ?> >Cear&aacute;</option>
							<option value="df" <?php if($obras->getUfObra() == 'df'){echo "selected='selected'";} ?> >Distrito Federal</option>
							<option value="es" <?php if($obras->getUfObra() == 'es'){echo "selected='selected'";} ?> >Esp&iacute;rito Santo</option>
							<option value="go" <?php if($obras->getUfObra() == 'go'){echo "selected='selected'";} ?> >Goi&aacute;s</option>
							<option value="ma" <?php if($obras->getUfObra() == 'ma'){echo "selected='selected'";} ?> >Maranh&atilde;o</option>
							<option value="mt" <?php if($obras->getUfObra() == 'mt'){echo "selected='selected'";} ?> >Mato Grosso</option>
							<option value="ms" <?php if($obras->getUfObra() == 'ms'){echo "selected='selected'";} ?> >Mato Grosso do Sul</option>
							<option value="mg" <?php if($obras->getUfObra() == 'mg'){echo "selected='selected'";} ?> >Minas Gerais</option>
							<option value="pa" <?php if($obras->getUfObra() == 'pa'){echo "selected='selected'";} ?> >Par&aacute;</option>
							<option value="pb" <?php if($obras->getUfObra() == 'pb'){echo "selected='selected'";} ?> >Para&iacute;ba</option>
							<option value="pr" <?php if($obras->getUfObra() == 'pr'){echo "selected='selected'";} ?> >Paran&aacute;</option>
							<option value="pe" <?php if($obras->getUfObra() == 'pe'){echo "selected='selected'";} ?> >Pernambuco</option>
							<option value="pi" <?php if($obras->getUfObra() == 'pi'){echo "selected='selected'";} ?> >Piau&iacute;</option>
							<option value="rj" <?php if($obras->getUfObra() == 'rj'){echo "selected='selected'";} ?> >Rio de Janeiro</option>
							<option value="rn" <?php if($obras->getUfObra() == 'rn'){echo "selected='selected'";} ?> >Rio Grande do Norte</option>
							<option value="ro" <?php if($obras->getUfObra() == 'ro'){echo "selected='selected'";} ?> >Rond&ocirc;nia</option>
							<option value="rs" <?php if($obras->getUfObra() == 'rs'){echo "selected='selected'";} ?> >Rio Grande do Sul</option>
							<option value="rr" <?php if($obras->getUfObra() == 'rr'){echo "selected='selected'";} ?> >Roraima</option>
							<option value="sc" <?php if($obras->getUfObra() == 'sc'){echo "selected='selected'";} ?> >Santa Catarina</option>
							<option value="se" <?php if($obras->getUfObra() == 'se'){echo "selected='selected'";} ?> >Sergipe</option>
							<option value="sp" <?php if($obras->getUfObra() == 'sp'){echo "selected='selected'";} ?> >S&atilde;o Paulo</option>
							<option value="to" <?php if($obras->getUfObra() == 'to'){echo "selected='selected'";} ?> >Tocantins</option>
						</select>
					</div>
				</div>
			</div>
	
			<div class='footer'>
				<button id='btnSalvar'>Salvar</button>
				<script type="text/javascript">
					$("#btnSalvar").click(function(){
						if($('#txtNomeObra').val() == '' || $('#selEngResponsavel').val() == '0' || $('#txtEndObra').val() == '' 
							|| $('#txtRegObra').val() == '' || $('#txtNroEndObra').val() == '' || $('#txtBairroObra').val() == ''
						    || $('#txtCidadeObra').val() == '' || $('#selEstado').val() == '0' ){
							 alert('Favor preencher todos os campos!', 'Atencao');
						}else{
							utils.ajax('obras/salvar', {
								id: $("#hidId").val(),
								nomeObra: $('#txtNomeObra').val(),
								engResponsavel: $('#selEngResponsavel').val(),
								regObra: $('#txtRegObra').val(),
								endObra: $('#txtEndObra').val(),
								nroEndObra: $('#txtNroEndObra').val(),
								bairroObra: $('#txtBairroObra').val(),
								cidadeObra: $('#txtCidadeObra').val(),
								ufObra: $('#selEstado').val()
								
								
									
							}, function(xml){
									erro = $(xml).find('erro').text();
									if(parseInt(erro) == 0){
										msg = "Obra salva com sucesso!";
	
										$('#hidId').val('');
										$('#txtNomeObra').val('');
										$('#selEngResponsavel').val('0');
										$('#selEngResponsavel').selectmenu();
										$('#txtRegObra').val('');
										$('#txtEndObra').val('');
										$('#txtNroEndObra').val('');
										$('#txtBairroObra').val('');
										$('#txtCidadeObra').val('');
										$('#selEstado').val('0');
										$('#selEstado').selectmenu();
										
										
									}
									else{
										msg = "Obra NAO salva com sucesso!";
									}
									alert(msg, 'Obras');
							});
						}
					});
				</script> 
			</div>
	</div>

<?php 
if(!$popup)
	include_once '../templates/footer.php';
?>